package edu.neu.cs6515.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.util.List;
import java.util.Map;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import lombok.Getter;

import edu.neu.cs6515.board.Hexagon;
import edu.neu.cs6515.board.HexagonBoard;
import edu.neu.cs6515.xml.Score;
import edu.neu.cs6515.xml.Tile;

/**
 * Class to render edu.neu.cs6515.xml.Player
 */
public class PlayerRenderer extends JPanel {

    @Getter
    private JLabel timeLabel;

    @Getter
    private JLabel messageLabel;

    private static final long serialVersionUID = 1L;

    private BoardRenderer boardRenderer;

    private TilesInHandRenderer tilesInHandRenderer;

    private ScoreRenderer scoreRenderer;

    /**
     * Constructor of PlayerRenderer,
     * 
     * @param player
     */
    public PlayerRenderer() {
        super();
        initializeComponents();
    }

	/**
	* get the {@link Hexagon} to {@link CellRenderer} map
	*/
	public Map<Hexagon, CellRenderer> getHexToCellMap(){
		return this.boardRenderer.getHexToCells();
	}

    /**
     * 
     * @param board
     *            the hexagonboard to be drawn
     * @param score
     *            the score to be rendered
     * @param tiles
     *            tiles in hand to be rendered
     */
    public void update(final HexagonBoard board, final Score score,
            final List<Tile> tiles) {
        boardRenderer.update(board);
        tilesInHandRenderer.update(tiles);
        scoreRenderer.update(score);
    }

    /**
	 * ----------------------------
	 *  |						   |
	 *  |						   |
	 *  |						   |
	 *  |	{@link BoardRenderer}  |
	 *  |						   |
	 *  |						   |
	 *  |						   |
	 *  |						   |
	 *  |						   |
	 *  |						   |
	 *--|--------------------------|
	 *  |{Message Label}		   |
	 *--|--------------------------|
	 *  |{Time count down}		   |
	 *--|--------------------------|
	 *  |					|	   |
	 *  |(@link TilesInHandRenderer})					|{@link ScoreRenderer}	   |
	 *  |					|	   |
	 *--|--------------------------|
     * initialize BoardRenderer, TilesInHandRenderer, and ScoreRenderer
     */
    private void initializeComponents() {
        this.boardRenderer = new BoardRenderer();
		this.timeLabel = new JLabel();
        this.messageLabel = new JLabel();
        this.tilesInHandRenderer = new TilesInHandRenderer();
        this.scoreRenderer = new ScoreRenderer();

        Container contentPaneForTilesAndScore = new JPanel();
        contentPaneForTilesAndScore.setLayout(new FlowLayout());
        contentPaneForTilesAndScore.add(tilesInHandRenderer);
        contentPaneForTilesAndScore.add(scoreRenderer);

		JPanel jp = new JPanel();
		jp.setLayout(new BorderLayout());
		jp.add(contentPaneForTilesAndScore, BorderLayout.CENTER);
		jp.add(this.timeLabel, BorderLayout.NORTH);

		JPanel jp2 = new JPanel();
		jp2.setLayout(new BorderLayout());
		jp2.add(this.messageLabel, BorderLayout.NORTH);
		jp2.add(jp,BorderLayout.CENTER);

        this.setLayout(new BorderLayout());
        JScrollPane jScrollPane = new JScrollPane(boardRenderer);
        this.add(jScrollPane, BorderLayout.CENTER);
        this.add(jp2, BorderLayout.SOUTH);
    }

	public int getSelectedIndex() {
		return this.tilesInHandRenderer.getSelectedIndex();
	}
}
